package com.fingard.dsp.bank.directbank.aibank01.request;

import java.lang.reflect.Field;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.aibank01.AIBANK01Base;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpReceiver;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import com.baidu.ub.msoa.container.support.governance.annotation.BundleService;
import com.baidu.ub.msoa.rpc.RPCProtocol;
import com.baixin.infra.head.RequestHead;
import com.baixin.infra.head.generator.RequestHeadGenerator;
import com.baixin.infra.utils.uid.GlobalUidGeneratorFactory;
import com.baixin.infra.utils.uid.exception.GlobalUidGeneratorException;
import com.baixin.infra.utils.uid.generator.GlobalUidGenerator;
import com.kayak.api.Payment;
import com.kayak.api.SinTranStu;
import com.kayak.request.PaymentRequest;
import com.kayak.request.SinTranStuRequest;
import com.kayak.response.PaymentResponse;
import com.kayak.response.SinTranStuResponse;
import com.kayakwise.sgp.method.cnaps.SeLiqu0001;
import com.kayakwise.sgp.method.cnaps.SeLiqu0002;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0001Req;
import com.kayakwise.sgp.object.request.cnaps.SeLiqu0002Req;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0001Resp;
import com.kayakwise.sgp.object.response.cnaps.SeLiqu0002Resp;

@Service
public class SendRequest extends AIBANK01Base {

    @BundleService(provider = "${msoa.SeLiqu.provider}", service = SeLiqu0001.SERVICE_NAME,
            version = "${msoa.SeLiqu.version}", protocol = RPCProtocol.NAVI2JSON,
            interfaceType = SeLiqu0001.class)
    private SeLiqu0001 client1;

    @BundleService(provider = "${msoa.SeLiqu.provider}",service = SeLiqu0002.SERVICE_NAME,
            version = "${msoa.SeLiqu.version}", protocol = RPCProtocol.NAVI2JSON,
            interfaceType = SeLiqu0002.class)
    private SeLiqu0002 client2;

    @BundleService(provider = "${msoa.qsl02.provider}", service = Payment.SERVICE_NAME,
            version = "${msoa.qsl02.version}", protocol = RPCProtocol.NAVI2JSON,
            interfaceType = Payment.class)
    private Payment paymentClient;

    @BundleService(provider = "${msoa.qsl02.provider}", service = SinTranStu.SERVICE_NAME,
            version = "${msoa.qsl02.version}", protocol = RPCProtocol.NAVI2JSON,
            interfaceType = SinTranStu.class)
    private SinTranStu sinTranStuClient;

    public SeLiqu0001Resp sendToBank(SeLiqu0001Req req) {
        SeLiqu0001Resp resp = null;
        try{
            RequestHeadGenerator requestHeadGenerator = initHead("Seliqu0001");
            req.setHead(requestHeadGenerator.requestHeadGen());
            resp = client1.seliqu0001Method(req);
        }catch(Exception e ){
            e.printStackTrace();
        }
        return resp;
    }

    public SeLiqu0002Resp sendToBank(SeLiqu0002Req req) {
        SeLiqu0002Resp resp = null;
        try{
            RequestHeadGenerator requestHeadGenerator = initHead("Seliqu0002");
            req.setHead(requestHeadGenerator.requestHeadGen());
            resp = client2.seliqu0002Method(req);
        }catch(Exception e ){
            e.printStackTrace();
        }
        return resp;
    }

    public PaymentResponse sendToBank(PaymentRequest req) {
        PaymentResponse resp = null;
        try{
            RequestHeadGenerator requestHeadGenerator = initHeads();
            req.setHead(requestHeadGenerator.requestHeadGen());
            req.setTranNo(requestHeadGenerator.requestHeadGen().getTransId());
            resp = paymentClient.send(req);
        }catch(Exception e ){
            e.printStackTrace();
        }
        return resp;
    }

    public SinTranStuResponse sendToBank(SinTranStuRequest req) {
        SinTranStuResponse resp = null;
        try{
            RequestHeadGenerator requestHeadGenerator = initHeads();
            req.setHead(requestHeadGenerator.requestHeadGen());
            req.setTranNo(requestHeadGenerator.requestHeadGen().getTransId());//渠道流水号
            resp = sinTranStuClient.send(req);
        }catch(Exception e ){
            e.printStackTrace();
        }
        return resp;
    }

    private static RequestHead initRequestHead(){
        RequestHead head = new RequestHead();
        return head;
    }

    private static RequestHeadGenerator initHeads() {
        RequestHeadGenerator requestHeadGenerator = new RequestHeadGenerator();
        String setTransId=getGlobalUidGenerator();
        requestHeadGenerator.setTransCode("0123456789");
        requestHeadGenerator.setServiceId("123456ABXXXYYY20170809");
        requestHeadGenerator.setTransId(setTransId);
        requestHeadGenerator.setSceneId("1234567890123456");
        requestHeadGenerator.setSubTransSeq("1234567891ABCDEFGHB0");
        requestHeadGenerator.setSubTransSeq("1234567891A000EFGHB0", 1);
        requestHeadGenerator.setUserId("name");
        requestHeadGenerator.setCustomerId("123456789012");
        requestHeadGenerator.setBranchId("123456");
        requestHeadGenerator.setSourceType("1006");
        requestHeadGenerator.setWsId("kdg84er");
        try {
            Field f = requestHeadGenerator.getClass().getDeclaredField("sourceServiceId");
            f.setAccessible(true);
            f.set(requestHeadGenerator, "123456ABNNNZZZ20170809");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        requestHeadGenerator.setSysDate(Format.Date8Format.format(new Date()));
        requestHeadGenerator.setSourceBranchNo("WSXCDERFVBGTYHNM");
        requestHeadGenerator.setDestBranchNo("YHNJUMKIJHGTRFVB");
        requestHeadGenerator.setPriority(0);
        requestHeadGenerator.setAsync(true);
        return requestHeadGenerator;
    }

    private static RequestHeadGenerator initHead(String transCode) {
        RequestHeadGenerator requestHeadGenerator = new RequestHeadGenerator();
        String setTransId=getGlobalUidGenerator();
        requestHeadGenerator.setTransCode(transCode);
        requestHeadGenerator.setServiceId("123456ABXXXYYY20170809");
        requestHeadGenerator.setTransId(setTransId);
        requestHeadGenerator.setSceneId("1234567890123456");
        requestHeadGenerator.setSubTransSeq("1234567891ABCDEFGHB0");
        requestHeadGenerator.setSubTransSeq("1234567891A000EFGHB0", 1);
        requestHeadGenerator.setUserId("name");
        requestHeadGenerator.setCustomerId("123456789012");
        requestHeadGenerator.setBranchId("123456");
        requestHeadGenerator.setSourceType("1006");
        requestHeadGenerator.setWsId("kdg84er");
        try {
            Field f = requestHeadGenerator.getClass().getDeclaredField("sourceServiceId");
            f.setAccessible(true);
            f.set(requestHeadGenerator, "123456ABNNNZZZ20170809");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        requestHeadGenerator.setSysDate(Format.Date8Format.format(new Date()));
        requestHeadGenerator.setSourceBranchNo("WSXCDERFVBGTYHNM");
        requestHeadGenerator.setDestBranchNo("YHNJUMKIJHGTRFVB");
        requestHeadGenerator.setPriority(0);
        requestHeadGenerator.setAsync(true);
        return requestHeadGenerator;
    }

    public static RequestHead initRequestHead(String transCode) {
        RequestHead requestHead = new RequestHead();
        String setTransId=getGlobalUidGenerator();
        requestHead.setTransCode(transCode);
        requestHead.setServiceId("123456ABXXXYYY20170809");
        requestHead.setTransId(setTransId);
        requestHead.setSceneId("1234567890123456");
        requestHead.setSubTransSeq("1234567891ABCDEFGHB0");
        requestHead.setSubTransSeq("1234567891A000EFGHB0");
        requestHead.setUserId("name");
        requestHead.setCustomerId("123456789012");
        requestHead.setBranchId("123456");
        requestHead.setSourceType("1006");
        requestHead.setWsId("kdg84er");
        try {
            Field f = requestHead.getClass().getDeclaredField("sourceServiceId");
            f.setAccessible(true);
            f.set(requestHead, "123456ABNNNZZZ20170809");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        requestHead.setSysDate(Format.Date8Format.format(new Date()));
        requestHead.setSourceBranchNo("WSXCDERFVBGTYHNM");
        requestHead.setDestBranchNo("YHNJUMKIJHGTRFVB");
        requestHead.setPriority(0);
        requestHead.setAsync(true);
        return requestHead;
    }

    public static String  getGlobalUidGenerator(){
        GlobalUidGenerator generator = GlobalUidGeneratorFactory.getGlobalUidGenerator();
        try{
            generator.setSystemAbbr("FFM");
            return generator.getGlobalUID("FFM");
        } catch (GlobalUidGeneratorException e) {
            e.printStackTrace();
            return "";
        }
    }
}
